package com.shchuangzhi.smartguard.core;

import com.elvishew.xlog.XLog;
import com.google.gson.Gson;
import com.google.gson.annotations.SerializedName;
import com.google.gson.reflect.TypeToken;
import com.shchuangzhi.smartguard.core.command.SyncMetaCommandRespHandler;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by j_hu on 7/26/17.
 */

public class InOutStats {
    private DBHelper dbHelper;
    private InStatsDataView inStatsView = new InStatsDataView();
    private static final InOutStats instance = new InOutStats();
    public static final int TYPE_ID = 1;
    public InOutStats() {

    }

    public List<String> getInStatsHeaders() {
        return inStatsView.getHeaders();
    }

    public void addDbHelper(DBHelper dbHelper) {
        this.dbHelper = dbHelper;
    }

    public List<InStatsItem> getInStats(String key) {
        List<InStatsItem> items = null;
        if (inStatsView.getData().containsKey(key)) {
            items = inStatsView.getData().get(key);
        } else {
            items = new ArrayList<InStatsItem>();
        }
        return items;
    }

    public ParseResult parse(String json, boolean onlyEmpty) {
        boolean canParse = true;
        ParseResult result = new ParseResult(false, "");

        if (onlyEmpty) {
            canParse = inStatsView.isEmpty();
        }
        if (canParse) {
            Gson gson = new Gson();
            SyncMetaCommandRespHandler.SyncMetaCommandRS<InStatsDataView> rs = (SyncMetaCommandRespHandler.SyncMetaCommandRS<InStatsDataView>)
                    gson.fromJson(json, new TypeToken<SyncMetaCommandRespHandler.SyncMetaCommandRS<InStatsDataView>>(){}.getType());
            if (rs != null && rs.getData() != null) {
                inStatsView = rs.getData();
                result = new ParseResult(true, rs.getLastMetaUpdateTime());
            }
        }
        if (!onlyEmpty && dbHelper != null && result.isParseOk()) {
            if (!dbHelper.updateStats(TYPE_ID, json, Clock.nowStr())) {
                result = new ParseResult(false, "");
            }
        }
        return result;
    }

    public static InOutStats getInstance() {
        return instance;
    }

    public class ParseResult {
        private boolean parseOk;
        private String parseTime;
        private ParseResult(boolean isOk, String parseTime) {
            this.parseOk = isOk;
            this.parseTime = parseTime;
        }
        public boolean isParseOk() {
            return parseOk;
        }
        public String getParseTime() {
            return parseTime;
        }
    }

    public class InStatsDataView {
        private List<String> headers = new ArrayList<>();
        private HashMap<String, List<InStatsItem>> data = new HashMap<>();

        public List<String> getHeaders() {
            return headers;
        }

        public HashMap<String, List<InStatsItem>> getData() {
            return data;
        }

        public boolean isEmpty() {
            return headers.size() == 0 || data.size() == 0;
        }
    }

    public class InStatsItem {
        private String grade;
        @SerializedName("class")
        private String clazz;
        private String health;
        private String watch;
        private String sick;
        private String in;
        private String out;
        @SerializedName("in_pct")
        private String inPct;

        public String getValue(int order) {
            switch(order) {
                case 0:return getGrade();
                case 1:return getClazz();
                case 2:return getHealth();
                case 3:return getWatch();
                case 4:return getSick();
                case 5:return getIn();
                case 6:return getOut();
                case 7:return getInPct();
                default:return "";
            }
        }

        public String getGrade() {
            return grade;
        }
        public String getClazz() {
            return clazz;
        }
        public String getHealth() {
            return health;
        }
        public String getWatch() {
            return watch;
        }
        public String getSick() {
            return sick;
        }
        public String getIn() {
            return in;
        }
        public String getOut() {
            return out;
        }
        public String getInPct() {
            return inPct;
        }
    }
}
